{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "No module named transwarp",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-d53a721e414c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mdatetime\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtranswarp\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     16\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtranswarp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweb\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mWSGIApplication\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mJinja2TemplateEngine\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mImportError\u001b[0m: No module named transwarp"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "__author__ = 'Michael Liao'\n",
    "\n",
    "'''\n",
    "A WSGI application entry.\n",
    "'''\n",
    "\n",
    "import logging; logging.basicConfig(level=logging.INFO)\n",
    "\n",
    "import os, time\n",
    "from datetime import datetime\n",
    "\n",
    "from transwarp import db\n",
    "from transwarp.web import WSGIApplication, Jinja2TemplateEngine\n",
    "\n",
    "from config import configs\n",
    "\n",
    "def datetime_filter(t):\n",
    "    delta = int(time.time() - t)\n",
    "    if delta < 60:\n",
    "        return u'1分钟前'\n",
    "    if delta < 3600:\n",
    "        return u'%s分钟前' % (delta // 60)\n",
    "    if delta < 86400:\n",
    "        return u'%s小时前' % (delta // 3600)\n",
    "    if delta < 604800:\n",
    "        return u'%s天前' % (delta // 86400)\n",
    "    dt = datetime.fromtimestamp(t)\n",
    "    return u'%s年%s月%s日' % (dt.year, dt.month, dt.day)\n",
    "\n",
    "# init db:\n",
    "db.create_engine(**configs.db)\n",
    "\n",
    "# init wsgi app:\n",
    "wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__)))\n",
    "\n",
    "template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates'))\n",
    "template_engine.add_filter('datetime', datetime_filter)\n",
    "\n",
    "wsgi.template_engine = template_engine\n",
    "\n",
    "import urls\n",
    "\n",
    "wsgi.add_interceptor(urls.user_interceptor)\n",
    "wsgi.add_interceptor(urls.manage_interceptor)\n",
    "wsgi.add_module(urls)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    wsgi.run(9000, host='0.0.0.0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
